#include "desktop_page.h"


static const char* TAG = "lvgl_project";

static lv_style_t style_bg;
static lv_style_t style_indic;

#define desktop_Button_Num 13

lv_obj_t* lvgl_desktop_main_cont = NULL;
lv_obj_t  lvgl_desktop_style;
lv_style_t lvgl_desktop_BeiJing_style;
lv_style_t lvgl_WuBianKuang_cont_style;

lv_obj_t* lvgl_desktop_Button[desktop_Button_Num];
lv_obj_t* lvgl_desktop_Label[desktop_Button_Num];
lv_style_t lvgl_desktop_Button_style[desktop_Button_Num];


lv_area_t desktop_BUTTON_area[] =
{
    {5,5,125,65},{135,5,100,65},
    {5,75,155,65},{165,75,70,65},
    {5,145,70,80},{80,145,155,80},
    {5,230,113,75},{123,230,112,75},
    {5,310,155,65},{165,310,70,65},
    {5,380,113,80},{123,380,113,80},
    {5,465,230,130},
};

static void btns_event_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* btn = lv_event_get_target(e);
    if (code == LV_EVENT_CLICKED) {
        lv_page_load(WEATHER_PAGE);
    }
}

void lv_desktop_create_demo(lv_obj_t* obj)
{
    printf("lv_desktop_create");
    lv_style_init(&lvgl_desktop_BeiJing_style);
    lv_style_init(&lvgl_desktop_style);
    //渐变色
    lv_style_set_bg_opa(&lvgl_desktop_BeiJing_style, LV_OPA_COVER);
    //设置控件背景色
    lv_style_set_bg_color(&lvgl_desktop_BeiJing_style, lv_color_black());
    lv_obj_add_style(lv_scr_act(), &lvgl_desktop_BeiJing_style, 0);//设置样式

    lv_style_set_bg_opa(&lvgl_desktop_style, LV_OPA_COVER);
    //设置控件背景色
    lv_style_set_bg_color(&lvgl_desktop_style, lv_color_black());
    lv_style_set_bg_grad_dir(&lvgl_desktop_style, LV_GRAD_DIR_VER);//渐变方向

    lv_style_set_bg_main_stop(&lvgl_desktop_style, 10);
    lv_style_set_bg_main_stop(&lvgl_desktop_style, 100);

    lvgl_desktop_main_cont = obj;

    lv_obj_set_pos(lvgl_desktop_main_cont, 0, 0);
    lv_obj_set_size(lvgl_desktop_main_cont, lv_obj_get_width(lv_scr_act()), 280);
    lv_obj_add_style(lvgl_desktop_main_cont, &lvgl_desktop_style, 0);

    for (int i = 0; i < desktop_Button_Num; i++)
    {
        lv_style_init(&lvgl_desktop_Button_style[i]);
        lv_style_init(&lvgl_WuBianKuang_cont_style);
        lv_style_set_radius(&lvgl_WuBianKuang_cont_style, 0);
        lv_style_set_border_opa(&lvgl_WuBianKuang_cont_style, 255);//边框透明度
        lv_style_set_border_width(&lvgl_WuBianKuang_cont_style, 0);//边框宽度
        lv_style_set_border_side(&lvgl_WuBianKuang_cont_style, LV_BORDER_SIDE_NONE);
        lv_style_set_pad_top(&lvgl_WuBianKuang_cont_style, 0);//内边距上侧尺寸
        lv_style_set_pad_bottom(&lvgl_WuBianKuang_cont_style, 0);//内边距下侧尺寸
        lv_style_set_pad_left(&lvgl_WuBianKuang_cont_style, 0);//内边距左侧尺寸
        lv_style_set_pad_right(&lvgl_WuBianKuang_cont_style, 0);//内边距右侧尺寸

        lvgl_desktop_Button[i] = lv_btn_create(lvgl_desktop_main_cont);
        lv_obj_set_pos(lvgl_desktop_Button[i], desktop_BUTTON_area[i].x1, desktop_BUTTON_area[i].y1);
        lv_obj_set_size(lvgl_desktop_Button[i], desktop_BUTTON_area[i].x2, desktop_BUTTON_area[i].y2);
        lv_obj_add_style(lvgl_desktop_Button[i], &lvgl_desktop_Button_style[i], 0);
        lv_obj_add_event_cb(lvgl_desktop_Button[i], btns_event_cb, LV_EVENT_ALL, NULL);
    }
}




lv_obj_t* container;

static void button_event_cb(lv_event_t* event);

void desktop_init(void)
{
    container = lv_obj_create(lv_scr_act());
    lv_obj_set_size(container, LV_PCT(100), LV_PCT(100));
    lv_obj_set_scrollbar_mode(container, LV_SCROLLBAR_MODE_OFF);
    lv_obj_set_flex_flow(container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(container, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_set_scroll_snap_x(container, LV_SCROLL_SNAP_CENTER);
    lv_obj_set_style_bg_opa(container, LV_OPA_0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(container, lv_color_black(), LV_PART_MAIN);
    lv_obj_set_style_border_width(container, 0, LV_PART_MAIN);
    lv_obj_set_style_pad_column(container, 60, LV_PART_MAIN);   //图标之间的间隙
    lv_obj_center(container);

    //生成演示按钮
    for (int i = 0; i < 10; i++)
    {
        lv_obj_t* btn = lv_btn_create(container);
        lv_obj_set_size(btn, 80, 80);
        lv_obj_add_event_cb(btn, button_event_cb, LV_EVENT_ALL, NULL);

        lv_obj_t* label = lv_label_create(btn);
        lv_label_set_text_fmt(label, "%d", i);
        lv_obj_center(label);
    }

    uint32_t mid_btn_index = (lv_obj_get_child_cnt(container) - 1) / 2;
    for (uint32_t i = 0; i < mid_btn_index; i++)
    {
        lv_obj_move_to_index(lv_obj_get_child(container, -1), 0);
    }
    /*当按钮数为偶数时，确保按钮居中*/
    lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index), LV_ANIM_OFF);
}


/**
 * @brief 处理按钮事件的回调函数
 * @param event
*/
static void button_event_cb(lv_event_t* event)
{
    lv_obj_t* current_btn = lv_event_get_current_target(event);
    uint32_t current_btn_index = lv_obj_get_index(current_btn);
    uint32_t mid_btn_index = (lv_obj_get_child_cnt(container) - 1) / 2;

    if (event->code == LV_EVENT_FOCUSED)
    {
        if (current_btn_index > mid_btn_index)
        {
            lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index - 1), LV_ANIM_OFF);
            lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index), LV_ANIM_ON);
            lv_obj_move_to_index(lv_obj_get_child(container, 0), -1);
        }
        else if (current_btn_index < mid_btn_index)
        {
            lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index + 1), LV_ANIM_OFF);
            lv_obj_scroll_to_view(lv_obj_get_child(container, mid_btn_index), LV_ANIM_ON);
            lv_obj_move_to_index(lv_obj_get_child(container, -1), 0);
        }
    }
    else if (event->code == LV_EVENT_CLICKED)
    {
        /**/
    }
}



static lv_style_t desktop_style;

static void desktop_event_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* cont = lv_event_get_target(e);
    lv_indev_wait_release(lv_indev_get_act());
    lv_dir_t dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    switch (dir)
    {
    case LV_DIR_TOP:
        printf("dir:LV_DIR_TOP\n");
        lv_page_load(WEATHER_PAGE);
        break;
    case LV_DIR_BOTTOM:
        printf("dir:LV_DIR_BOTTOM\n");
        break;
    case LV_DIR_RIGHT:
        printf("dir:LV_DIR_RIGHT\n");
        break;
    case LV_DIR_LEFT:
        printf("dir:LV_DIR_LEFT\n");
        break;
    default:
        break;
    }
}




void lv_desktop_create(lv_obj_t* obj)
{
    printf("lv_desktop_create");
    lv_style_init(&lvgl_desktop_BeiJing_style);
    lv_style_init(&lvgl_desktop_style);
    //渐变色
    lv_style_set_bg_opa(&lvgl_desktop_BeiJing_style, LV_OPA_COVER);
    //设置控件背景色
    lv_style_set_bg_color(&lvgl_desktop_BeiJing_style, lv_color_black());
    lv_obj_add_style(lv_scr_act(), &lvgl_desktop_BeiJing_style, 0);//设置样式

    lv_style_set_bg_opa(&lvgl_desktop_style, LV_OPA_COVER);
    //设置控件背景色
    lv_style_set_bg_color(&lvgl_desktop_style, lv_color_black());
    lv_style_set_bg_grad_dir(&lvgl_desktop_style, LV_GRAD_DIR_VER);//渐变方向

    lv_style_set_bg_main_stop(&lvgl_desktop_style, 10);
    lv_style_set_bg_main_stop(&lvgl_desktop_style, 100);

    lvgl_desktop_main_cont = obj;

    lv_obj_set_pos(lvgl_desktop_main_cont, 0, 0);
    lv_obj_set_size(lvgl_desktop_main_cont, lv_obj_get_width(lv_scr_act()), 280);
    lv_obj_add_style(lvgl_desktop_main_cont, &lvgl_desktop_style, 0);

    for (int i = 0; i < desktop_Button_Num; i++)
    {
        lv_style_init(&lvgl_desktop_Button_style[i]);
        lv_style_init(&lvgl_WuBianKuang_cont_style);
        lv_style_set_radius(&lvgl_WuBianKuang_cont_style, 0);
        lv_style_set_border_opa(&lvgl_WuBianKuang_cont_style, 255);         //边框透明度
        lv_style_set_border_width(&lvgl_WuBianKuang_cont_style, 0);         //边框宽度
        lv_style_set_border_side(&lvgl_WuBianKuang_cont_style, LV_BORDER_SIDE_NONE);
        lv_style_set_pad_top(&lvgl_WuBianKuang_cont_style, 0);              //内边距上侧尺寸
        lv_style_set_pad_bottom(&lvgl_WuBianKuang_cont_style, 0);           //内边距下侧尺寸
        lv_style_set_pad_left(&lvgl_WuBianKuang_cont_style, 0);             //内边距左侧尺寸
        lv_style_set_pad_right(&lvgl_WuBianKuang_cont_style, 0);            //内边距右侧尺寸

        lvgl_desktop_Button[i] = lv_btn_create(lvgl_desktop_main_cont);
        lv_obj_set_pos(lvgl_desktop_Button[i], desktop_BUTTON_area[i].x1, desktop_BUTTON_area[i].y1);
        lv_obj_set_size(lvgl_desktop_Button[i], desktop_BUTTON_area[i].x2, desktop_BUTTON_area[i].y2);
        lv_obj_add_style(lvgl_desktop_Button[i], &lvgl_desktop_Button_style[i], 0);
        lv_obj_add_event_cb(lvgl_desktop_Button[i], btns_event_cb, LV_EVENT_ALL, NULL);
    }
}


void lv_desktop_entry_ani(lv_obj_t* obj)
{
    lv_anim_t ani;
    lvAnimationMovDown(&ani, obj, LV_VER_RES);
    lv_obj_clear_flag(obj, LV_OBJ_FLAG_HIDDEN);
}


//动画结束后隐藏当前界面
void desktop_check(struct _lv_anim_t* ani)
{
    lv_obj_t* obj = ani->user_data;
    lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
}

void lv_desktop_exit_ani(lv_obj_t* obj)
{
    lv_anim_t ani;
    lv_anim_init(&ani);
    lv_anim_set_ready_cb(&ani,desktop_check);
    lv_anim_set_var(&ani, obj);
    lv_anim_set_values(&ani, 0, LV_VER_RES);
    lv_anim_set_exec_cb(&ani, lv_obj_set_y);
    lv_anim_set_time(&ani, 1000);
    lv_anim_set_user_data(&ani, obj);
    //  lv_anim_set_playback_time(&a, 2000);
    lv_anim_start(&ani);
    //lv_obj_add_flag(obj, LV_OBJ_FLAG_HIDDEN);
}
